package com.yz.jdd.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yz.jdd.R;
import com.yz.jdd.base.utils.Tools;


/**
 * Created by shibin on 2017/1/9.
 * 多种样式弹窗
 */

public class CustomDialog extends Dialog {
    public CustomDialog(Context context) {
        super(context);
    }

    public CustomDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    public static class Builder {
        private Context context;
        private String title;
        private String message;
        private String message1;
        private String positiveButtonText;//确定按钮
        private String negativeButtonText;//取消按钮
        private boolean isShowClose = false;//是否显示右上角关闭按钮
        private boolean isBackCancelable;//物理键返回是否取消
        private DialogInterface.OnClickListener positiveButtonClickListener;
        private DialogInterface.OnClickListener negativeButtonClickListener;
        private DialogInterface.OnClickListener closeButtonClickListener;
        private LinearLayout progressLayout;//进度view

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }


        public Builder setMessage(int message) {
            this.message = (String) context.getText(message);
            return this;
        }

        public Builder setMessage1(String message1) {
            this.message1 = message1;
            return this;
        }


        public Builder setMessage1(int message1) {
            this.message1 = (String) context.getText(message1);
            return this;
        }

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setTitle(int title) {
            this.title = (String) context.getText(title);
            return this;
        }

        public Builder setCloseButton(DialogInterface.OnClickListener listener) {
            isShowClose = true;
            this.closeButtonClickListener = listener;
            return this;
        }

        public Builder setPositiveButton(int positiveButtonText,
                                         DialogInterface.OnClickListener listener) {
            this.positiveButtonText = (String) context
                    .getText(positiveButtonText);
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setPositiveButton(String positiveButtonText,
                                         DialogInterface.OnClickListener listener) {
            this.positiveButtonText = positiveButtonText;
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(int negativeButtonText,
                                         DialogInterface.OnClickListener listener) {
            this.negativeButtonText = (String) context
                    .getText(negativeButtonText);
            this.negativeButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(String negativeButtonText,
                                         DialogInterface.OnClickListener listener) {
            this.negativeButtonText = negativeButtonText;
            this.negativeButtonClickListener = listener;
            return this;
        }

        public Builder setOnKeyCancelable(boolean isCancelable) {

            this.isBackCancelable = isCancelable;
            return this;
        }

        /**
         * 单选dialog
         *
         * @return
         */
        public CustomDialog createChoiceDialog() {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final CustomDialog dialog = new CustomDialog(context, R.style.Dialog);
            View layout = inflater.inflate(R.layout.dialog_custom_choice, null);
            dialog.addContentView(layout, new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            dialog.setCanceledOnTouchOutside(false);
            Window dialogWindow = dialog.getWindow();
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            dialogWindow.setGravity(Gravity.CENTER);
            lp.width = (int) (Tools.getScreenWidth((Activity) context) * 0.8);
            dialogWindow.setAttributes(lp);
            if (!isBackCancelable) {
                dialog.setOnKeyListener(new OnKeyListener() {
                    @Override
                    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
                            return true;
                        } else {
                            return false;
                        }
                    }
                });
            }
            // 标题
            if (!TextUtils.isEmpty(title)) {
                ((TextView) layout.findViewById(R.id.title)).setText(title);
                layout.findViewById(R.id.title).setVisibility(View.VISIBLE);
            }

            // 确认按钮
            if (positiveButtonText != null) {
                ((Button) layout.findViewById(R.id.positiveButton))
                        .setText(positiveButtonText);
                if (positiveButtonClickListener != null) {
                    ((Button) layout.findViewById(R.id.positiveButton))
                            .setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    positiveButtonClickListener.onClick(dialog,
                                            DialogInterface.BUTTON_POSITIVE);
                                }
                            });
                }
            } else {
                layout.findViewById(R.id.positiveButton).setVisibility(
                        View.GONE);
            }
            // 取消按钮
            if (negativeButtonText != null) {
                ((Button) layout.findViewById(R.id.negativeButton))
                        .setText(negativeButtonText);
                if (negativeButtonClickListener != null) {
                    ((Button) layout.findViewById(R.id.negativeButton))
                            .setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    negativeButtonClickListener.onClick(dialog,
                                            DialogInterface.BUTTON_NEGATIVE);
                                }
                            });
                }
            } else {
                layout.findViewById(R.id.negativeButton).setVisibility(
                        View.GONE);
            }

            if (!TextUtils.isEmpty(message)) {
                ((TextView) layout.findViewById(R.id.message)).setText(message);
                layout.findViewById(R.id.message).setVisibility(View.VISIBLE);
            }


            dialog.setContentView(layout);
            return dialog;
        }


        //变化后的布局
        public void setChangeMessage(String msg) {
            messageTV.setText(!TextUtils.isEmpty(msg) ? msg : "");
            messageTV.setVisibility(View.VISIBLE);
            initMessageTv.setVisibility(View.GONE);
            changeLayout.setVisibility(View.VISIBLE);
            progressLayout.setVisibility(View.VISIBLE);
        }

        //变化前的布局
        public void setInitMessage(String msg) {
            initMessageTv.setText(msg);
            initMessageTv.setVisibility(View.VISIBLE);
        }

        private TextView messageTV, initMessageTv;//变化以前的文字 变化后的文字
        private LinearLayout changeLayout;//变化以前的布局


        /**
         * 确认取消 带图片
         */
        public CustomDialog confirmDialogWithImage() {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final CustomDialog dialog = new CustomDialog(context, R.style.Dialog);
            View layout = inflater.inflate(R.layout.dialog_custom_confirm_image, null);
            dialog.addContentView(layout, new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            dialog.setCanceledOnTouchOutside(false);
            Window dialogWindow = dialog.getWindow();
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            dialogWindow.setGravity(Gravity.CENTER);
            lp.height = (int) (Tools.getScreenHeight((Activity) context) * 0.3f);
            lp.width = (int) (Tools.getScreenWidth((Activity) context) * 0.8f);
            dialogWindow.setAttributes(lp);

            progressLayout = (LinearLayout) layout.findViewById(R.id.progress_layout);
            progressLayout.setVisibility(View.GONE);
            messageTV = (TextView) layout.findViewById(R.id.message_tv);
            initMessageTv = (TextView) layout.findViewById(R.id.init_message_tv);
            changeLayout = (LinearLayout) layout.findViewById(R.id.change_layout);

            changeLayout.setVisibility(View.GONE);


            if (!isBackCancelable) {
                dialog.setOnKeyListener(new OnKeyListener() {
                    @Override
                    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
                            return true;
                        } else {
                            return false;
                        }
                    }
                });
            }
            // 标题
            if (!TextUtils.isEmpty(title)) {
                ((TextView) layout.findViewById(R.id.title)).setText(title);
                layout.findViewById(R.id.title).setVisibility(View.VISIBLE);
            } else {
                layout.findViewById(R.id.title).setVisibility(View.GONE);
            }


            // 确认按钮
            if (positiveButtonText != null) {
                ((Button) layout.findViewById(R.id.positiveButton))
                        .setText(positiveButtonText);
                if (positiveButtonClickListener != null) {
                    ((Button) layout.findViewById(R.id.positiveButton))
                            .setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    positiveButtonClickListener.onClick(dialog,
                                            DialogInterface.BUTTON_POSITIVE);
                                }
                            });
                }
            } else {
                layout.findViewById(R.id.positiveButton).setVisibility(
                        View.GONE);
            }
            // 取消按钮
            if (negativeButtonText != null) {
                ((Button) layout.findViewById(R.id.negativeButton))
                        .setText(negativeButtonText);
                if (negativeButtonClickListener != null) {
                    ((Button) layout.findViewById(R.id.negativeButton))
                            .setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    negativeButtonClickListener.onClick(dialog,
                                            DialogInterface.BUTTON_NEGATIVE);
                                }
                            });
                }
            } else {
                layout.findViewById(R.id.negativeButton).setVisibility(
                        View.INVISIBLE);
            }

            dialog.setContentView(layout);
            return dialog;
        }



        /**
         * 确认、取消dialog
         *
         * @return
         */
        public CustomDialog createConfirmNagativeDialog() {
            return createConfirmNagativeDialog(R.layout.dialog_custom_confirm_nagative, 0.3f, 0.8f);
        }
        public CustomDialog createConfirmDialog() {
            return createConfirmNagativeDialog(R.layout.dialog_custom_confirm_nagative, 0.46f, 0.8f);
        }

        public CustomDialog createConfirmNagativeDialog(int layoutId, float scaleHeight, float scaleWight) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final CustomDialog dialog = new CustomDialog(context, R.style.Dialog);
            View layout = inflater.inflate(layoutId, null);
            dialog.addContentView(layout, new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            dialog.setCanceledOnTouchOutside(false);
            Window dialogWindow = dialog.getWindow();
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            dialogWindow.setGravity(Gravity.CENTER);
            lp.height = (int) (Tools.getScreenHeight((Activity) context) * scaleHeight);
            lp.width = (int) (Tools.getScreenWidth((Activity) context) * scaleWight);
            dialogWindow.setAttributes(lp);
            if (!isBackCancelable) {
                dialog.setOnKeyListener(new OnKeyListener() {
                    @Override
                    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
                            return true;
                        } else {
                            return false;
                        }
                    }
                });
            }
            // 标题
            if (!TextUtils.isEmpty(title)) {
                ((TextView) layout.findViewById(R.id.title)).setText(title);
                layout.findViewById(R.id.title).setVisibility(View.VISIBLE);
            }


            // 确认按钮
            if (positiveButtonText != null) {
                ((Button) layout.findViewById(R.id.positiveButton))
                        .setText(positiveButtonText);
                if (positiveButtonClickListener != null) {
                    ((Button) layout.findViewById(R.id.positiveButton))
                            .setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    positiveButtonClickListener.onClick(dialog,
                                            DialogInterface.BUTTON_POSITIVE);
                                }
                            });
                }
            } else {
                layout.findViewById(R.id.positiveButton).setVisibility(
                        View.GONE);
            }
            // 取消按钮
            if (negativeButtonText != null) {
                ((Button) layout.findViewById(R.id.negativeButton))
                        .setText(negativeButtonText);
                if (negativeButtonClickListener != null) {
                    ((Button) layout.findViewById(R.id.negativeButton))
                            .setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    negativeButtonClickListener.onClick(dialog,
                                            DialogInterface.BUTTON_NEGATIVE);
                                }
                            });
                }
            } else {
                layout.findViewById(R.id.negativeButton).setVisibility(
                        View.GONE);
            }

            //关闭按钮
            if (isShowClose) {
                layout.findViewById(R.id.pay_loading_close).setVisibility(View.VISIBLE);
                layout.findViewById(R.id.pay_loading_close).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        closeButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEUTRAL);
                    }
                });
            }

            if (!TextUtils.isEmpty(message)) {
                ((TextView) layout.findViewById(R.id.message)).setText(message);
                layout.findViewById(R.id.message).setVisibility(View.VISIBLE);
            }

            if (!TextUtils.isEmpty(message1)) {
                ((TextView) layout.findViewById(R.id.message1)).setText(message1);
                layout.findViewById(R.id.message1).setVisibility(View.VISIBLE);
            }

            dialog.setContentView(layout);
            return dialog;
        }
    }


}
